package com.luckyframe.project.jmeter.execution;

import com.alibaba.fastjson.JSON;
import com.luckyframe.project.jmeter.consumer.watch.PlanWatchTask;
import com.luckyframe.project.jmeter.report.domain.Chart;
import com.luckyframe.project.jmeter.report.domain.Line;
import org.apache.jmeter.report.dashboard.JsonizerVisitor;
import org.apache.jmeter.report.processor.*;
import org.apache.jmeter.report.processor.graph.impl.TransactionsPerSecondGraphConsumer;
import org.apache.jmeter.testelement.TestStateListener;
import org.apache.jmeter.util.JMeterUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TestPlanStateListenerImpl implements TestStateListener {

    private PlanWatchTask planWatchTask;

    public TestPlanStateListenerImpl(PlanWatchTask planWatchTask) {
        this.planWatchTask = planWatchTask;
    }

    @Override
    public void testStarted() {
        if(planWatchTask!=null){
            planWatchTask.start();
        }
    }

    @Override
    public void testStarted(String host) {

    }

    @Override
    public void testEnded() {
        if(planWatchTask!=null){
            planWatchTask.stop();
        }
    }

    @Override
    public void testEnded(String host) {

    }





    public static void main(String[] args) {

        String JMETER_HOME = "D:\\eptok\\LuckyFrameWeb\\src\\main\\resources\\jmeter";

        String JMETER_PROPERTIES = JMETER_HOME + "/bin/jmeter.properties";
        JMeterUtils.loadJMeterProperties(JMETER_PROPERTIES);
        JMeterUtils.setJMeterHome(JMETER_HOME);


//        OverallStatisticsSummaryConsumer overallStatisticsSummaryConsumer=new OverallStatisticsSummaryConsumer ();
//        overallStatisticsSummaryConsumer.setName("overallStatistics");
//        overallStatisticsSummaryConsumer.setHasOverallResult(false);

        SampleContext sampleContext = new SampleContext();


        TransactionsPerSecondGraphConsumer transactionsPerSecondGraphConsumer = new TransactionsPerSecondGraphConsumer();
        transactionsPerSecondGraphConsumer.setGranularity(10000);
        transactionsPerSecondGraphConsumer.setName("tpsOverTime");
        transactionsPerSecondGraphConsumer.initialize();

        CsvFileSampleSource csvFileSampleSource = new CsvFileSampleSource(new File("C:\\Users\\lancelot\\Documents\\WXWork\\1688851860186281\\Cache\\File\\2020-11\\httptest111902.jtl"), ',');

        csvFileSampleSource.addSampleConsumer(transactionsPerSecondGraphConsumer);

        JsonizerVisitor jsonizerVisitor = new JsonizerVisitor();

        csvFileSampleSource.setSampleContext(sampleContext);

        csvFileSampleSource.run();


        Map<String, Object> map = sampleContext.getData();

        List<Chart> chars=new ArrayList<>();

        for (String key : map.keySet()) {

            Chart chart=new Chart(key);
            chars.add(chart);

            MapResultData mapResultData = (MapResultData) map.get(key);
            ListResultData series = (ListResultData) mapResultData.getResult("series");

            if (series.getSize() > 0) {

                List<Line> lines=new ArrayList<>();
                chart.setLines(lines);

                for (int j = 0; j < series.getSize(); j++) {

                    MapResultData resultData = (MapResultData) series.get(j);
                    ValueResultData label = (ValueResultData) resultData.getResult("label");

                    Line line=new Line(label.getValue().toString());
                    lines.add(line);

                    ListResultData data = (ListResultData) resultData.getResult("data");

                    if (data.getSize() > 0) {

                        for (int i = 0; i < data.getSize(); i++) {
                            ListResultData pointData = (ListResultData) data.get(i);
                            line.addPoint(toPoint(pointData));

                        }
                    }
                }
            }
        }

        System.out.println("chars = " + JSON.toJSONString(chars));


    }

    private static Double[] toPoint(ListResultData listResultData) {

        ValueResultData xval = (ValueResultData)listResultData.get(0);
        ValueResultData yval = (ValueResultData)listResultData.get(1);

        Double x=Double.valueOf(xval.getValue().toString());
        Double y=Double.valueOf(yval.getValue().toString());


        return new Double[]{x,y};

    }


}
